package cn.edu.jssvc.rollcall;

import android.content.Context;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.Window;

public class BaseActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow();
        //设置全屏显示
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        isNetworkAvailable(this);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK){
            finish();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    private void isNetworkAvailable(Context context) {
        boolean isNetwork = false;
        try {
            ConnectivityManager conn = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            if (null == conn || null == conn.getActiveNetworkInfo()){
                isNetwork = false;
                showDialog(getResources().getString(R.string.isNetworkAvailable));
            } else {
                isNetwork = true;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    private void showDialog(String message) {
        new AlertDialog.Builder(this)
                .setTitle(getResources().getString(R.string.tip))
                .setMessage(message)
                .setPositiveButton(getResources().getString(R.string.ok),null)
                .show();
    }
}
